package com.yzf.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yzf.entity.Contact;
import com.yzf.entity.Project;
import com.yzf.service.ContactService;
import com.yzf.util.util.DateTimeUtil;
import com.yzf.util.web.ServletUtil;

public class EditContactServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String id = request.getParameter("id");
		
		ContactService service = new ContactService();
		Contact contact = service.findById(id);
		
		request.setAttribute("contact", contact);
		ServletUtil.forword(request, response, "edit_contact");
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String id = request.getParameter("id");
		ContactService service = new ContactService();
		Contact contact = service.findById(id);
		
		HttpSession session = ServletUtil.getSession(request);
		Project  project = (Project)session.getAttribute("project"); 
		
		String name = request.getParameter("name");
		String company = request.getParameter("company");
		String tel = request.getParameter("tel");
		String moblie = request.getParameter("moblie");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		String website = request.getParameter("website");
		String weiboName = request.getParameter("weibo");
		String content = request.getParameter("content");
		String weiboType = request.getParameter("weiboType");
		
		String weibo =   weiboType + ":" + weiboName;
		
		
		
		contact.setId(id);
		contact.setName(name);
		contact.setCompanyname(company);
		contact.setTel(tel);
		contact.setMoblie(moblie);
		contact.setEmail(email);
		contact.setAddress(address);
		contact.setWebsite(website);
		contact.setWeibo(weibo);
		contact.setContent(content);
		contact.setCreatetime(DateTimeUtil.getNow());
		contact.setProjectid(project.getId());
		
		//request.setAttribute("contact", contact);
		
		service.edit(contact);
		
		
		ServletUtil.redirect(response, "contactShow.jspx");
	}

}
